package com.xm.common.entity.vo.request;

import com.fasterxml.jackson.annotation.JsonFormat;
import jakarta.validation.constraints.NotNull;
import jakarta.validation.constraints.Pattern;
import lombok.Data;

import java.time.Duration;
import java.time.LocalDateTime;

@Data
public class MonitorQueryParam {
    @NotNull(message = "客户端ID不能为空")
    private Integer clientId;

    @NotNull(message = "统计类型不能为空")
    @Pattern(regexp = "AVG|MAX|MIN|LAST", message = "无效的统计类型")
    private String statType;

    @NotNull(message = "指标类型不能为空")
    @Pattern(regexp = "CPU|MEMORY|DISK|NETWORK|DISK-IO", message = "无效的指标类型")
    private String metricType;

    @NotNull(message = "开始时间不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime start;

    @NotNull(message = "结束时间不能为空")
    @JsonFormat(pattern = "yyyy-MM-dd'T'HH:mm:ss")
    private LocalDateTime end;

    public String getTableName() {
        long duration = Duration.between(start, end).toMinutes();
        if (duration <= 60) {
            return "history_" + statType.toLowerCase() + "_1m";
        } else if (duration <= 360) {
            return "history_" + statType.toLowerCase() + "_5m";
        } else if (duration <= 720) {
            return "history_" + statType.toLowerCase() + "_10m";
        } else if (duration <= 1440) {
            return "history_" + statType.toLowerCase() + "_20m";
        } else if (duration <= 4320) {
            return "history_" + statType.toLowerCase() + "_1h";
        } else if (duration <= 10080) {
            return "history_" + statType.toLowerCase() + "_2h";
        } else if (duration <= 20160) {
            return "history_" + statType.toLowerCase() + "_6h";
        } else if (duration <= 43200) {
            return "history_" + statType.toLowerCase() + "_12h";
        } else {
            throw new IllegalArgumentException("时间范围不能超过30天");
        }
    }
}
